<template>
  <div>
    <ingredient ref="ingredient" v-for="item in dataList" :key="item.id" :CookieTemplateList="item" :orderDetail="item.orderDetail" style="width: 350px;height: 150px;"></ingredient>
  </div>
</template>

<script>
import ingredient from './components/ingredient'
import { printingDisabledCtrl, getLoadPromise } from '@/util/print'
import getLodop from '@/lodop/LodopFuncs';
import { query } from "@/api/user"; // 查询生产打印机设备
export default {
  data() {
    return {
      dataList: []
    };
  },
  methods: {
    async print({dataList, isPreview}) {
      try {
        printingDisabledCtrl(true)
        this.dataList = dataList
        const printerRes = await this.queryPrinter()
        if(printerRes.data.data && printerRes.data.data.length) {
          const printerList = JSON.parse(printerRes.data.data[0].value)
          await getLoadPromise(1000)
          const LODOP = getLodop()
          if (LODOP) {
            LODOP.PRINT_INIT('') //初始化
            LODOP.SET_PRINT_PAGESIZE(0, '101.6mm', '60mm', 'USER')
            const printCards = this.$refs.ingredient
            if (printCards && printCards.length) {
              printCards.forEach(info => {
                LODOP.NewPage()
                LODOP.ADD_PRINT_HTM('1%', '1%', '98%', '98%', info.$el.outerHTML) //设置打印内容
              })
            }
            CLODOP.On_Return=function(TaskID,value){
              console.log(value)
            }
            LODOP.SET_PRINTER_INDEX(printerList[0]); // 指定打印机
            LODOP.SET_PRINT_MODE('CUSTOM_TASK_NAME', '生产订单-配料表')// 打印任务名
            if (isPreview) {
              LODOP.PREVIEW()
            } else {
              LODOP.PRINT()
            }
          }
          printingDisabledCtrl(false)
          this.$notify({
            title: '打印成功',
            message: '请您耐心等待',
            type: 'success'
          })
          return true
        } else {
          throw new Error('请到系统模块中的打印机设置中添加打印机')
        }
      } catch (error) {
        console.error(error)
        this.$message.warning(error.message || '打印失败，请检查是否安装好驱动和已经连接打印机，或重新打印！');
        printingDisabledCtrl(false)
        return false
      }
    },
    queryPrinter() {
      let formInline = {
        type: 1,
        key: '[1,2]',
        userId: JSON.parse(window.localStorage.getItem("saber-userInfo")).content.account
      }
      return query(formInline)
    }
  },
  components: {
    ingredient
  }
};
</script>
<style lang="scss" scoped>

</style>